/**
 * @FileName: AddDeviceActivity.java
 * @Package:com.bdtop.dbl.ui.activities.device
 * @Description: TODO
 * @author: MingjunLi
 * @date:2014-9-15
 * @version V1.0
 */

package com.bdtop.dbl.ui.activities.device;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.bdtop.dbl.BaseActivity;
import com.bdtop.dbl.DBLApplication;
import com.bdtop.dbl.R;
import com.bdtop.dbl.ui.activities.main.MainActivity;
import com.bdtop.dbl.ui.view.TitleBar;
import com.bdtop.dbl.utils.Preferences;
import com.zbar.lib.CaptureActivity;

/**
 * @ClassName: AddDeviceActivity
 * @Description: TODO
 * @author: MingjunLi
 * @date:2014-9-15
 */
public class DeviceAddTipActivity extends BaseActivity {

	private TitleBar mTitleBar;

	private Button mAddDeviceBtn;
	private Button mBuyBtn;

	// private Button mLookBtn;

	public static final void launch(Context context) {
		Intent intent = new Intent(context, DeviceAddTipActivity.class);
		((Activity) context).startActivityForResult(intent, MainActivity.ADD_DEVICE_FROM_MAIN);
		((Activity) context).overridePendingTransition(R.anim.slide_in_bottom, R.anim.keep_original_state);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {

			DeviceInfoActivity.launch(DeviceAddTipActivity.this, DBLApplication.getInstance().currentDi);

			DeviceAddTipActivity.this.setResult(RESULT_OK);
			DeviceAddTipActivity.this.finish();
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_add_device);
		initViews();
	}

	private void initViews() {
		mTitleBar = (TitleBar) findViewById(R.id.title_bar);
		mTitleBar.setTitle(R.string.add_device_title);
		mTitleBar.setBelongActivity(this);

		mAddDeviceBtn = (Button) findViewById(R.id.add_device_btn);
		mAddDeviceBtn.setOnClickListener(mOnClickListener);

		mBuyBtn = (Button) findViewById(R.id.buy_btn);
		mBuyBtn.setOnClickListener(mOnClickListener);

		// mLookBtn = (Button) findViewById(R.id.look_btn);
		// mLookBtn.setOnClickListener(mOnClickListener);
	}

	private OnClickListener mOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.add_device_btn:
//				MipcaActivityCapture.launch(DeviceAddTipActivity.this);
				// fix : change ZXings to ZBar
				CaptureActivity.launch(DeviceAddTipActivity.this);
				break;
			case R.id.buy_btn:
				showToast(R.string.buy_tips);
				break;
			// case R.id.look_btn:
			// clearLoginParams();
			// MainActivity.launch(DeviceAddTipActivity.this);
			// finish();
			// break;
			}
		}
	};

	@Override
	public void finish() {
		super.finish();
		overridePendingTransition(R.anim.keep_original_state, R.anim.slide_out_bottom);
	}

	public void clearLoginParams() {
//		String username = Preferences.getUserName(this);
//		Preferences.saveUserName(this, "");
//		Preferences.savePassword(this, "");
//		Preferences.setAutoLogin(this, false);
//		Preferences.setRememberPwd(this, false);
//		Preferences.saveLoginStatus(this, false);
//		Preferences.saveLastLoginUser(this, username);
//
//		DBLApplication.getInstance().deviceInfoList = null;
//		DBLApplication.getInstance().currentDi = null;
		
		
		
		String username = Preferences
				.getUserName(this);
		// fix: 注销 保留 账号密码
//		Preferences.saveUserName(getActivity(), "");
//		Preferences.savePassword(getActivity(), "");
//		Preferences.setRememberPwd(getActivity(), false);
		
		// fix: 一次性commit
		// Preferences.setAutoLogin(getActivity(), false);
		// Preferences.saveLoginStatus(getActivity(), false);
		// Preferences.saveLastLoginUser(getActivity(),username);
		
//		Editor editor = Preferences.getPreferences(this).edit();
//		editor.putBoolean(Preferences.AUTO_LOGIN, false);
//		editor.putBoolean(Preferences.ISLOGIN, false);
//		editor.putString(Preferences.LAST_LOGIN_USER, username);
//		editor.commit();
		// fix: db
		Preferences.clearLoginParams(username);

		DBLApplication.getInstance().deviceInfoList = null;
		DBLApplication.getInstance().currentDi = null;
	}
}
